package sk.stuba.fiit.foo07.genex.common;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

public class ResourceHelper {
    public byte[] getResourceBytes(String resPath) {
        byte imgbytes[] = null;
        try {
            InputStream is = getClass().getResourceAsStream(resPath);
            imgbytes = new byte[is.available()];
            is.read(imgbytes);
        } catch (IOException ex) {
            // TODO dorobit odchytavanie vynimiek (a premysliet)

        }
        return imgbytes;
    }

    /**
     * Returns String with macros from given filename
     * 
     * @param filename
     *                filename of file with macros to be read
     * @return
     */
    public String getMacros(String filename) {
        StringBuffer data = null;
        try {
            BufferedReader input = new BufferedReader(new FileReader("macros/"
                    + filename));
            data = new StringBuffer();
            String line = null;
            try {
                while ((line = input.readLine()) != null) {
                    data.append(line);
                    data.append(System.getProperty("line.separator"));
                }
            } finally {
                input.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        return data.toString();
    }

    public String getMacros() {
        return getMacros("default.ltx");
    }
}
